package com.ruoyi.fitsculpt.utils;

import com.ruoyi.fitsculpt.domain.EasyfitUsers;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class TokenUtils {

    private static final String SECRET_KEY = "auction_secret_key";
    private static final long EXPIRE_TIME = 24 * 60 * 60 * 1000; // 24小时

    public static String generateToken(EasyfitUsers user) {
        Date now = new Date();
        Date expireDate = new Date(now.getTime() + EXPIRE_TIME);

        return Jwts.builder()
                .setSubject(user.getUserId().toString())
                .setIssuedAt(now)
                .setExpiration(expireDate)
                .signWith(SignatureAlgorithm.HS512, SECRET_KEY)
                .compact();
    }

    public static Long getUserIdFromToken(String token) {
        try {
            String userId = Jwts.parser()
                    .setSigningKey(SECRET_KEY)
                    .parseClaimsJws(token)
                    .getBody()
                    .getSubject();
            return Long.valueOf(userId);
        } catch (Exception e) {
            return null;
        }
    }
}
